package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.node.LayuiTreeNode;
import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.BtyZxHuopinFenlei;
import cn.stylefeng.guns.sys.modular.system.entity.ZxKemu;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyZxHuopinFenleiMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyZxHuopinFenleiParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyZxHuopinFenleiResult;
import cn.stylefeng.guns.sys.modular.system.service.BtyZxHuopinFenleiService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 装修货品分类表 服务实现类
 * </p>
 *
 * @author liuy
 * @since 2021-03-11
 */
@Service
public class BtyZxHuopinFenleiServiceImpl extends ServiceImpl<BtyZxHuopinFenleiMapper, BtyZxHuopinFenlei> implements BtyZxHuopinFenleiService {

    @Override
    public void add(BtyZxHuopinFenleiParam param){
        param.setCreateUser(LoginContextHolder.getContext().getUserId());
        BtyZxHuopinFenlei entity = getEntity(param);

        this.save(entity);
    }

    @Override
    public void delete(BtyZxHuopinFenleiParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(BtyZxHuopinFenleiParam param){
        BtyZxHuopinFenlei oldEntity = getOldEntity(param);
        BtyZxHuopinFenlei newEntity = getEntity(param);
        newEntity.setUpdateUser(LoginContextHolder.getContext().getUserId());
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public BtyZxHuopinFenleiResult findBySpec(BtyZxHuopinFenleiParam param){
        return null;
    }

    @Override
    public List<BtyZxHuopinFenleiResult> findListBySpec(BtyZxHuopinFenleiParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(BtyZxHuopinFenleiParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(BtyZxHuopinFenleiParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private BtyZxHuopinFenlei getOldEntity(BtyZxHuopinFenleiParam param) {
        return this.getById(getKey(param));
    }

    private BtyZxHuopinFenlei getEntity(BtyZxHuopinFenleiParam param) {
        BtyZxHuopinFenlei entity = new BtyZxHuopinFenlei();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    @Override
    public List<LayuiTreeNode> layuiTree() {
        return this.baseMapper.layuiTree();
    }

    @Override
    public List<ZTreeNode> tree() {
        return this.baseMapper.tree();
    }

    @Override
    public List<LayuiTreeNode> layuiTreeDgd() {
        return this.baseMapper.layuiTreeDgd();
    }

    @Override
    public List<ZTreeNode> treeDgd() {
        return this.baseMapper.treeDgd();
    }

    @Override
    public BtyZxHuopinFenlei getDetailById(Long id) {
        BtyZxHuopinFenlei detail = this.getById(id);
        if (detail.getPid() != 0L && detail != null) {
            BtyZxHuopinFenlei parentDetail = this.getById(detail.getPid());
            if (parentDetail != null) {
                detail.setPName(parentDetail.getName());
            }
        }
        return detail;
    }

    @Override
    public List<BtyZxHuopinFenleiResult> getHpFlList(BtyZxHuopinFenleiParam huopinFenleiParam) {
        return baseMapper.customList(huopinFenleiParam);
    }

}
